package com.example.dell.library.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.dell.library.R;
import com.example.dell.library.activity.BookInforActivity;
import com.example.dell.library.activity.ImgActivity;
import com.example.dell.library.activity.LoginActivity;
import com.example.dell.library.activity.PasswordActivity;
import com.example.dell.library.bean.Student;
import com.example.dell.library.fragment.HomeFragment;
import com.google.gson.Gson;

public class MineFragment extends Fragment {
    private TextView stuNo;
    private TextView stuName;
    private Button btnImg;
    private Button btnPassword;
    private Button btnExit;
    private ImageView stuImg;
    Student student;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_mine, container, false);
        String StuJson = getActivity().getIntent().getStringExtra("STUDENT");
        student = new Gson().fromJson(StuJson, Student.class);

        stuNo = (TextView) view.findViewById(R.id.stu_no);
        stuNo.setText(student.getStuNo());

        stuName = (TextView) view.findViewById(R.id.stu_name);
        stuName.setText(student.getName());

        stuImg = (ImageView) view.findViewById(R.id.stu_img);
        Glide.with(getContext()).load(Uri.parse(student.getImgFile().getFileUrl())).into(stuImg);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
//        btnImg = (Button) getActivity().findViewById(R.id.btn_img);
//        btnImg.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

        btnPassword = (Button) getActivity().findViewById(R.id.btn_password);
        btnPassword.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intentP = new Intent(getActivity(), PasswordActivity.class);
                startActivity(intentP);
            }
        });

        btnExit = (Button) getActivity().findViewById(R.id.btn_exit);
        btnExit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext());
                alertDialog.setTitle("确定退出登录吗？");
                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                });
                alertDialog.setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                alertDialog.show();
            }
        });
    }
}
